package com.gobang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gobang.domain.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户数据访问层
 * 
 * @author gobang
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名查询用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    /**
     * 更新用户积分和游戏统计
     *
     * @param userId 用户ID
     * @param newScore 新积分
     * @param isWin 是否获胜
     * @return 更新行数
     */
    @Update("UPDATE users SET current_score = #{newScore}, " +
            "total_games = total_games + 1, " +
            "win_games = win_games + #{isWin}, " +
            "updated_at = NOW() " +
            "WHERE id = #{userId}")
    int updateUserGameStats(@Param("userId") Long userId, 
                           @Param("newScore") Integer newScore,
                           @Param("isWin") Integer isWin);

    /**
     * 检查用户名是否存在
     *
     * @param username 用户名
     * @return 存在返回1，不存在返回0
     */
    @Select("SELECT COUNT(1) FROM users WHERE username = #{username}")
    int existsByUsername(@Param("username") String username);
} 